package com.zhongshengnetwork.rightbe.lang.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.activity.MyWebActivity;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.loading.KyLoadingBuilder;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.SmartRankMyInfo;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;

import java.util.HashMap;
import java.util.Map;

public class SmartRankSettingsActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener {

    private TopBarView topbar;
    private TextView enable_text;
    private boolean enable;
    private KyLoadingBuilder load = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_rank_settings);

        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);

        RelativeLayout smart_rank_about = (RelativeLayout) findViewById(R.id.smart_rank_about);
        smart_rank_about.setClickable(true);
        smart_rank_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent webIntent = new Intent(SmartRankSettingsActivity.this, MyWebActivity.class);
                webIntent.putExtra(APIKey.urlKey, Constant.HOST+"api/system/smartrank.do");
                startActivity(webIntent);//用户协议
            }
        });

        enable = SPManageUtils.getInstance(CustomApplication.mContext).getSPBoolean(AdvertUtils.Smart_Rank_State, false);

        enable_text = (TextView) findViewById(R.id.enable_text);
        if (enable == false){
            enable_text.setText("立即开启");
        }else {
            enable_text.setText("立即进入");
        }
        enable_text.setClickable(true);
        enable_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (enable == true){
                    finish();
                    Intent intent = new Intent(CustomApplication.mContext, SmartRankActivity.class);
                    startActivity(intent);
                }else {
                    CustomDialog dialog = null;
                    final CustomDialog.Builder builder = new CustomDialog.Builder(SmartRankSettingsActivity.this);
                    builder.setTitle("温馨提示");
                    builder.setMessage("亲，开启智慧排行后，你将参与每日排名，并且可以查看微友们的动态排名信息哦，如需开启请按确定。");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            load = new KyLoadingBuilder(SmartRankSettingsActivity.this);
                            load.setIcon(R.drawable.loading04);
                            load.setText("开启中...");
                            load.setOutsideTouchable(false);//点击空白区域是否关闭
                            load.setBackTouchable(true);//按返回键是否关闭
                            load.show();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    final Map<String,String> map = new HashMap<String, String>();
                                    map.put("dataStatus","1");
                                    map.put("token",CustomApplication.loginModel.getToken());
                                    String url = "rank/smart/settting.do";
                                    HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                                        @Override
                                        public void onSuccess(String data) {
                                            CommonModel commonModel = GsonTools.getCommonModel(data);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    load.dismiss();
                                                    if (commonModel.getFlag().equals("1")){
                                                        ToastUtil.show(SmartRankSettingsActivity.this, "开启成功");
                                                        enable = true;
                                                        SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(AdvertUtils.Smart_Rank_State, true);
                                                        enable_text.setText("立即进入");
                                                        finish();
                                                        Intent intent = new Intent(CustomApplication.mContext, SmartRankActivity.class);
                                                        startActivity(intent);
                                                    }else {
                                                        CustomApplication.showTip(commonModel, SmartRankSettingsActivity.this);
                                                    }
                                                }
                                            });
                                        }

                                        @Override
                                        public void onError(String msg) {
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    load.dismiss();
                                                }
                                            });
                                        }
                                    });
                                }
                            }).start();
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    dialog = builder.create();
                    dialog.show();
                }
            }
        });



        load = new KyLoadingBuilder(SmartRankSettingsActivity.this);
        load.setIcon(R.drawable.loading04);
        load.setText("加载中...");
        load.setOutsideTouchable(false);//点击空白区域是否关闭
        load.setBackTouchable(true);//按返回键是否关闭
        load.show();

        //获取排名信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Map<String,String> map = new HashMap<String, String>();
                map.put("token",CustomApplication.loginModel.getToken());
                String url = "rank/smart/info.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                load.dismiss();
                                CommonModel commonModel = GsonTools.getCommonModel(data);
                                if (commonModel.getFlag().equals("1")){
                                    SmartRankMyInfo smartRankMyInfo = GsonTools.getSmartRankMyInfoOfAll(data);

                                    if (smartRankMyInfo != null){
                                        SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(AdvertUtils.Smart_Rank_State, smartRankMyInfo.isStatus());
                                        enable = smartRankMyInfo.isStatus();
                                        if (enable == false){
                                            enable_text.setText("立即开启");
                                        }else {
                                            enable_text.setText("立即进入");
                                        }
                                    }

                                }else {
                                    CustomApplication.showTip(commonModel, SmartRankSettingsActivity.this);
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(String msg) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                load.dismiss();
                            }
                        });

                    }
                });
            }
        }).start();

    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        finish();
        //Toast.makeText(LangDetailActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();

    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

}
